#include "drv_recoder.h"
#include "sp_flash.h"
#define  CALIB_MAX	10
//device [addr][baund][x]
//sensor [lmt_h][lmt_l][calib_num][calib_needfla][calib[max]][use] 2+2+1+1+10+1
void drv_recoder_write_buf(uint32_t addr,_sensor_t *t)
{
	uint8_t buf[50]={0};
	uint8_t	boo=0;
	buf[0]=t->lmt_buf.lmt_h>>8;
	buf[1]=t->lmt_buf.lmt_h;
	buf[2]=t->lmt_buf.lmt_l>>8;
	buf[3]=t->lmt_buf.lmt_l;
	buf[4]=t->calib_buf.calib_num;
	buf[5]=t->calib_buf.needcalib_fla;
	for(boo=0;boo<CALIB_MAX;boo++)
	{
		buf[6+boo*4]=t->calib_buf.calib_arr[boo].x>>8;
		buf[7+boo*4]=t->calib_buf.calib_arr[boo].x;
		buf[8+boo*4]=t->calib_buf.calib_arr[boo].y>>8;
		buf[9+boo*4]=t->calib_buf.calib_arr[boo].y;
	}//9+9*4=9+36=45
	buf[46]=t->sta.use;
	__disable_irq();
	sp_flash_write_buf(addr,buf, 50);
	__enable_irq();
}
void drv_recoder_read_buf(uint32_t addr,_sensor_t *t)
{
	uint8_t buf[50]={0};
	uint8_t boo=0;
	__disable_irq();
	sp_flash_read_buf(addr,buf, 50);
	__enable_irq();
	t->lmt_buf.lmt_h=buf[0]<<8|buf[1];
	t->lmt_buf.lmt_l=buf[2]<<8|buf[3];
	t->calib_buf.calib_num=buf[4];
	t->calib_buf.needcalib_fla=buf[5];
	for(boo=0;boo<CALIB_MAX;boo++)
	{
		t->calib_buf.calib_arr[boo].x=buf[6+boo*4]<<8|buf[7+boo*4];
		t->calib_buf.calib_arr[boo].y=buf[8+boo*4]<<8|buf[9+boo*4];
	}
	t->sta.use=buf[46];

}
void drv_recoder_write_device_buf(uint32_t addr,_device_t *t)
{
	uint8_t buf[10]={0};
	buf[0]=t->addr;
	buf[1]=t->baud;
	buf[2]=t->status;
	__disable_irq();
	sp_flash_write_buf(addr,buf, 10);
	__enable_irq();
}
void drv_recoder_read_device_buf(uint32_t addr,_device_t *t)
{
	uint8_t buf[10]={0};
		__disable_irq();
	sp_flash_read_buf(addr,buf, 10);
		__enable_irq();
	t->addr=	buf[0];
	t->baud=  buf[1];
	t->status=buf[2];
}

void drv_have_recoder_init_buf(void)//restore factory
{
	uint8_t buf[10]={0};
	uint8_t boo=0;
	for(boo=0;boo<10;boo++)
	{
		buf[boo]=0;
	}
	__disable_irq();
	sp_flash_write_buf(HAVE_RECODER_ADD,buf, 10);
	__enable_irq();
}

void drv_have_recoder_write_buf(void)
{
	uint8_t buf[10]={0};
	uint8_t boo=0;
	for(boo=0;boo<10;boo++)
	{
		buf[boo]=boo;
	}
	__disable_irq();
	sp_flash_write_buf(HAVE_RECODER_ADD,buf, 10);
	__enable_irq();
}
uint8_t drv_have_recoder_read_buf(void)
{
		uint8_t boo=0;
	uint8_t res=0;
		uint8_t buf[10]={0};
	__disable_irq();
	sp_flash_read_buf(HAVE_RECODER_ADD,buf, 10);
	__enable_irq();
		for(boo=0;boo<10;boo++)
		{
			if(buf[boo]!=boo)
			{
				res=1;
				break;
			}
		}
		return res;
}